// 请求基地址
const baseURL = 'https://pcapi-xiaotuxian-front-devtest.itheima.net'

//拦截器配置
const httpInterceptor = {
  //要看具体逻辑来知道这样操作url是啥意思
  invoke(options: UniApp.RequestOptions) {
    if (!options.url.startsWith('http')) {
      options.url = baseURL + options.url
    }

    options.timeout = 10000

    options.header = {
      'source-client': 'miniapp', //注意，这是从小程序向服务器发送请求时，服务器判断请求来源的标识，如果是app，就填写app
      ...options.header,
    }

    const memberStore = useMemberStore()
    const token = memberStore.profile?.token
    if (token) {
      options.header.Authorization = token
    }
  },
}

//拦截request请求
uni.addInterceptor('request', httpInterceptor)

//拦截upload请求
uni.addInterceptor('uploadFile', httpInterceptor)
